package com.todo.controller;

import com.todo.common.Result;
import com.todo.utils.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;

/**
 * 短信测试控制器
 * 仅用于开发和测试环境
 */
@Slf4j
// @RestController
//@RequestMapping("/api/test/sms")
//@CrossOrigin(origins = "*")
public class SmsTestController {

    @Autowired
    private SmsUtil smsUtil;
    
    /**
     * 测试发送短信
     */
    @PostMapping("/send")
    public Result<String> testSendSms(
            @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String mobile,
            @RequestParam String content) {
        try {
            log.info("测试发送短信，手机号：{}，内容：{}", mobile, content);
            
            SmsUtil.SmsResult result = smsUtil.sendSms(mobile, content);
            
            if (result.isSuccess()) {
                return Result.success("短信发送成功：" + result.getMessage());
            } else {
                return Result.error("短信发送失败：" + result.getMessage());
            }
        } catch (Exception e) {
            log.error("测试发送短信异常", e);
            return Result.error("短信发送异常：" + e.getMessage());
        }
    }
    
    /**
     * 测试发送验证码短信
     */
    @PostMapping("/send-code")
    public Result<String> testSendVerificationCode(
            @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String mobile) {
        try {
            log.info("测试发送验证码短信，手机号：{}", mobile);
            
            // 生成测试验证码
            String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
            
            SmsUtil.SmsResult result = smsUtil.sendVerificationCode(mobile, code);
            
            if (result.isSuccess()) {
                return Result.success("验证码发送成功：" + result.getMessage() + "，验证码：" + code);
            } else {
                return Result.error("验证码发送失败：" + result.getMessage());
            }
        } catch (Exception e) {
            log.error("测试发送验证码短信异常", e);
            return Result.error("验证码发送异常：" + e.getMessage());
        }
    }
} 